<template>
  <div class="login-container">
    <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
      <h3 class="title">单一窗口一次申报</h3>
      <div class="buttonTab">
        <el-radio-group v-model="isTest" size="small" change="changeEnv">
          <el-radio-button label="正式" ></el-radio-button>
          <el-radio-button label="测试"></el-radio-button>
        </el-radio-group>
      </div>
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
        <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="username" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on"
          placeholder="password"></el-input>
          <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
          Sign in
        </el-button>
      </el-form-item>
      <el-tooltip class="item" effect="light" content="点击设置" placement="top-start">
      <div class="buttonwrap">
        <el-button size="mini" type="text" >当前文件路径：</el-button><div class="pathUrl">
        {{curPath}}
      </div>
        <input  type="file" webkitdirectory class="input-file" @change="filePathChange($event.target.files)">
      </div>
      </el-tooltip>
      
    </el-form>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import { JSEncrypt } from 'jsencrypt'
import store from '@/store/index'
export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 2) {
        callback(new Error('密码不能小于2位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'ddjd',
        password: '123'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      curPath: store.getters.path,
      pwdType: 'password',
      isTest: store.getters.istest === '1' ? '测试' : '正式'
    }
  },
  mounted() {
  },
  watch: {
    isTest(value) {
      this.$store.dispatch('ToggleHost', value === '正式' ? '0' : '1')
      console.log(store.getters.istest)
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    filePathChange(fileList) {
      if (!fileList.length) return
      this.curPath = fileList[fileList.length - 1].path
      this.$store.dispatch('PathSet', this.curPath)
    },
    getEncrypted(text) {
      const PUBLIC_KEY = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJrd1c438F5eQekhhKyPuoK4MuTYfwr87bob4AtYG3g8oYC2dfXsE/P1exP0hK1C8vemWjzx6oSDZU6xCbzPmy0CAwEAAQ=='
      const encryptor = new JSEncrypt()
      encryptor.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----')
      const enc = encryptor.encrypt(text)
      if (enc !== false) {
        return enc
      }
      return enc
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          console.log(this.loginForm)
          let {
            password
          } = this.loginForm
          password = this.getEncrypted(password)
          this.$store.dispatch('Login', {
            ...this.loginForm,
            password
          }).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;
.buttonTab{
  position: absolute;
  right: 35px;
  top: 360px
}
.buttonwrap{
  position: relative;
  margin-bottom: 10px
}
.pathUrl{
  color: #fff;
  word-break: break-all;
  font-size: 12px;
  margin-bottom: 10px;
  display:inline-block;
  margin-left: 10px;
}
.input-file {
  left: 0;
  bottom: 0;
  top: 0;
  right: 0;
  opacity: 0; /* invisible but it's there! */
  position: absolute;
  cursor: pointer;
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
